function Array2D(x,y)
{
	 this.items = new Array(x);
	 for(i=0;i<y;i++){this.items[i] = new Array(y);}
};

function arrayToCol(arr){
	return "rgb(" +Math.round(arr[0])+ "," +Math.round(arr[1])+ "," +Math.round(arr[2])+ ")";
};

function pressureToCol(pres){
	return arrayToCol(mix2([255,255,255],[0,0,0],pres));
};


function getRandomColor() {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
};

var Game = {};

Game.fps = 10;
Game.isRunning = true;
Game.player1 = new Player(14,14, 20);
Game.player2 = new Player(1,1, -12);
Game.field = new Array(16);
Game.hasPowerUp = false;
Game.powerup;
Game.powerUpSpawnTimer = 50;

Game.emitColor = function(x,y,col){
	Game.field[x][y] = col; 
};

Game.updateField = function(){
	var oldField = Game.field.slice(0);
	
	for (var x = 1; x < 15; x = x + 1)
	{
		for(var y = 1; y < 15; y = y + 1)
		{
			var surrounding = 		(oldField[x-1][y-1]+	oldField[x][y-1]+	oldField[x+1][y-1]+
									 oldField[x-1][y]+							oldField[x+1][y]+
									 oldField[x-1][y+1]+	oldField[x][y+1]+	oldField[x+1][y+1]) / 8;
			/*var surrounding = mix(	oldField[x-1][y-1],	oldField[x][y-1],	oldField[x+1][y-1],
									oldField[x-1][y],						oldField[x+1][y],
									oldField[x-1][y+1],	oldField[x][y+1],	oldField[x+1][y+1]);*/
									
			Game.field[x][y] = (oldField[x][y] + 3*surrounding)/4; //mix2( oldField[x][y], surrounding, 0.5);
		}	                                                     
	}

}

Game.initialize = function() {
	this.context = document.getElementById("view").getContext("2d");
	this.context.clearRect(0, 0, 800, 800);
	this.context.fillStyle= "rgb(125,125,125)";
	this.context.fillRect(0,0,800,800);
	
	this.context.fillStyle= "rgb(0,0,0)";
	
	this.context.fillRect(5*50,4*50,50,50);
	this.context.fillRect(6*50,4*50,50,50);
	this.context.fillRect(7*50,4*50,50,50);
	this.context.fillRect(5*50,5*50,50,50);
	this.context.fillRect(5*50,6*50,50,50);
	this.context.fillRect(5*50,7*50,50,50);
	this.context.fillRect(6*50,7*50,50,50);
	this.context.fillRect(7*50,7*50,50,50);
	this.context.fillRect(7*50,6*50,50,50);
	
	this.context.fillRect(9*50,4*50,50,50);
	this.context.fillRect(10*50,4*50,50,50);
	this.context.fillRect(11*50,4*50,50,50);
	this.context.fillRect(9*50,5*50,50,50);
	this.context.fillRect(9*50,6*50,50,50);
	this.context.fillRect(9*50,7*50,50,50);
	this.context.fillRect(10*50,7*50,50,50);
	this.context.fillRect(11*50,7*50,50,50);
	this.context.fillRect(11*50,6*50,50,50);
	this.context.fillRect(11*50,5*50,50,50);
	
	this.context.font = "bold 20px Arial";
	for (var x = 0; x < 16; x = x + 1)
	{
		Game.field[x] = new Array(16);
		for(var y = 0; y < 16; y = y + 1)
		{
			Game.field[x][y] = 0.5; //[100,0,100];
		}	
	}
};

Game.draw = function() {
	
	if(!Game.isRunning)
		return;

	this.context.clearRect(0, 0, 800, 800);
	for (var x = 0; x < 16; x = x + 1)
	{
		for(var y = 0; y < 16; y = y +1)
		{
			if(Game.field[x][y] > 1)
				Game.player2.points--;
			if(Game.field[x][y] < 0)
				Game.player1.points--;
				
			this.context.fillStyle = pressureToCol(Game.field[x][y]); //arrayToCol(Game.field[x][y]);
			this.context.fillRect(x*50,y*50,50,50);
		}	
	}
	
	if(Game.hasPowerUp)
	{
		this.context.fillStyle= "rgb(0,255,0)";
		this.context.fillRect(Game.powerUp.x*50,Game.powerUp.y*50,50,50);
	}
	
	this.context.fillStyle= "rgb(255,0,0)";
	this.context.fillRect(Game.player1.x*50,Game.player1.y*50,50,50);
	
	this.context.fillStyle= "rgb(0,0,255)";
	this.context.fillRect(Game.player2.x*50,Game.player2.y*50,50,50);
	
	this.context.fillStyle= "rgb(255,0,0)";
	var i = 0;
	for(i = 0; i < Math.floor(Game.player1.points/6000); i++)
	{
		this.context.fillRect( (15 -(i))*50,0,50,50);
	}
	
	this.context.fillStyle= "rgba(255,0,0," + (Game.player1.points%6000)/6000 + ")";
	this.context.fillRect((15 -(i))*50,0,50,50);
	
	this.context.fillStyle= "rgb(0,0,255)";
	var i = 0;
	for(i = 0; i < Math.floor(Game.player2.points/6000); i++)
	{
		this.context.fillRect( (i)*50,0,50,50);
	}
	
	this.context.fillStyle= "rgba(0,0,255," + (Game.player2.points%6000)/6000 + ")";
	this.context.fillRect((i)*50,0,50,50);
};

Game.reset = function()
{
	Game.isRunning = true;
	Game.player1 = new Player(14,14, 20);
	Game.player2 = new Player(1,1, -12);
	Game.field = new Array(16);
	
	Game.initialize();
}

Game.update = function()
{
	if(!Game.isRunning)
		return;
	
	Game.powerUpSpawnTimer--;
	if(Game.powerUpSpawnTimer <= 0)
	{
		Game.powerUpSpawnTimer = 50;
		Game.hasPowerUp = true;
		Game.powerUp = new PowerUp(Math.round(Math.random() * 13) + 1, Math.round(Math.random() * 13) + 1);
	}
	
	if(Game.hasPowerUp)
	{
		if(Game.player1.x == Game.powerUp.x && Game.player1.y == Game.powerUp.y)
		{
			Game.powerUpSpawnTimer = 0;
			Game.hasPowerUp = false;
			Game.player1.power +=4;
		}
		
		if(Game.player2.x == Game.powerUp.x && Game.player2.y == Game.powerUp.y)
		{
			Game.powerUpSpawnTimer = 0;
			Game.hasPowerUp = false;
			Game.player2.power -=4;
		}
	}
	
	if(Game.player1.points <= 0)
	{
		this.context.font = "bold 40px Arial";
		this.context.fillStyle= "rgb(0,0,255)";
		this.context.fillRect(0,0,800,800);
		Game.player1.points = 0;
		Game.isRunning = false;
	}
	
	if(Game.player2.points <= 0)
	{
		this.context.font = "bold 40px Arial";
		this.context.fillStyle= "rgb(255,0,0)";
		this.context.fillRect(0,0,800,800);
		Game.player2.points = 0;
		Game.isRunning = false;
	}
		
	Game.emitColor(Game.player1.x,Game.player1.y,Game.player1.power);
	Game.emitColor(Game.player2.x,Game.player2.y,Game.player2.power);
	Game.updateField();
};